//
//  BrandViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/1.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "BrandViewController.h"
#import "HomeHeaderCollectionViewCell.h"
#import "ShopInfoViewController.h"
#import "GoodInfoViewController.h"
#import "SearchViewController.h"
#import "GoodModel.h"

@interface BrandViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) SDCycleScrollView *sdcView;

@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation BrandViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] GET:self.urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseState"] integerValue] == 0) {
            self.dataArray = [NSArray yy_modelArrayWithClass:[GoodModel class] json:responseObject[@"obj"]];
            [self.collectionView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation BrandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self.view addSubview:self.collectionView];
    if (self.isShowSearch) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:0 target:self action:@selector(searchEvent)];
        self.navigationItem.titleView = ({
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH, 30)];
            btn.layer.borderWidth = .5;
            btn.layer.borderColor = [UIColor whiteColor].CGColor;
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 3;
            [btn setTitle:@"搜索商品" forState:UIControlStateNormal];
            btn.titleLabel.font = DEF_FontSize_12;
            [btn addTarget:self action:@selector(searchEvent) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }else{
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button_turncrosswise"] style:0 target:self action:@selector(shopInfoEvent)];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"店铺详情" style:0 target:self action:@selector(shopInfoEvent)];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:({
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 24)];
            [btn setTitle:@"店铺详情" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = DEF_FontSize_12;
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 3;
            btn.layer.borderWidth = .5;
            btn.layer.borderColor = [UIColor whiteColor].CGColor;
            [btn addTarget:self action:@selector(shopInfoEvent) forControlEvents:UIControlEventTouchUpInside];
            btn;

        })];
    }
}

#pragma mark - Event

- (void)shopInfoEvent{
    ShopInfoViewController *vc = [ShopInfoViewController new];
    vc.sId = [self.urlString componentsSeparatedByString:@"="][1];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)searchEvent{
    SearchViewController *vc =[SearchViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeHeaderCollectionViewCell class]) forIndexPath:indexPath];
    cell.dataDictionary = self.dataArray[indexPath.row];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
        self.sdcView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160/WIDTH_5S_SCALE - 5)];
        
        NSString *urlString = [NSURL URLOffsetIPWithString:self.imgUrl].absoluteString;
        self.sdcView.imageURLStringsGroup = @[urlString];
        [view addSubview:self.sdcView];
    }
    return view;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodInfoViewController *vc = [GoodInfoViewController new];
    vc.gId = [self.dataArray[indexPath.row] gId];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - set & get

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.itemSize = CGSizeMake((SCREEN_WIDTH-5)/2., 135/WIDTH_5S_SCALE);
        layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 160/WIDTH_5S_SCALE);
        
        CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64);
        _collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerClass:[HomeHeaderCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeHeaderCollectionViewCell class])];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
    }
    return _collectionView;
}


@end
